<?php
/**
 * 获取产品数据写入数据库
 * Created by wzb.
 * User: wzb
 * time:2017/08/21
 */

/************处理产品信息*******************/


require("interface.php");
//脚本接收参数
fwrite(STDOUT,'请输入您的sku：');
$input_info = fgets(STDIN);
echo '您输入的sku是：'.$input_info;
if(empty($input_info) || $input_info == ""){
    exit;
}else{
$app_info = "select * from dlz_account_info";
$query = $db->query($app_info);//返回影响了多少行数据
while($row = $query->fetch()){
        $id_m = $row['id'];
        $store_name_m = $row['store_name'];
        $token_m = $row['token'];
        $baseUrl_m = $row['baseUrl'];
        $username_m = $row['username'];
        $password_m = $row['password'];
        $Mazentop = new Mazentop($token_m,$baseUrl_m,$username_m,$password_m);
        $sku = trim($input_info);
        
        Getproductinfo($Mazentop,$id_m,$store_name_m,$db,$sku);
    }
}
function Getproductinfo($Mazentop,$id_m,$store_name_m,$db,$sku){  
            $UpdateTime = date('Y-m-d H:i:s');
            echo "*************start get product*******************\n";
            $value = $Mazentop->getproductExtensionAttr($sku);

           // $getproduct_arr = $getproducts['items'];
            $zuzhuangarrary = array();
            //print_r($getproduct_arr);
            
                unset($zuzhuangarrary);
                $zuzhuangarrary['store_id'] =  $id_m;
                $zuzhuangarrary['store_name'] =  $store_name_m;
                $product_id = $value['id'];
                $zuzhuangarrary['product_id'] =  $product_id;
                $sku = $value['sku'];
                $zuzhuangarrary['sku'] =  $sku;
                $zuzhuangarrary['UpdateTime'] =  $UpdateTime;
                $name = $value['name'];
                $zuzhuangarrary['name'] =  $name;
                $storeid_sku = $id_m.'-'.$sku;
                $zuzhuangarrary['storeid_sku'] = $storeid_sku; 
                $attribute_set_id = $value['attribute_set_id'];//属性id
                $product_category = $Mazentop->getattributeSetId($attributeSetId=$attribute_set_id);
                $category = $product_category['attribute_set_name'];//类目
                $zuzhuangarrary['attribute_set_id'] =  $category;
                $price = $value['price'];
                $zuzhuangarrary['price'] =  $price;
                $status = $value['status'];
                $zuzhuangarrary['status'] =  $status;
                $created_at = $value['created_at'];
                $zuzhuangarrary['created_at'] =  $created_at;
                $updated_at = $value['updated_at'];
                $zuzhuangarrary['updated_at'] =  $updated_at;
                $weight = $value['weight'];
                $zuzhuangarrary['weight'] =  $weight;
                $options_arr = $value['options'];
                echo "*************start sku:$sku*******************\n";
                
                /***扩展属性****/
                $custom_attributes = $value['custom_attributes'];
                //print_r($custom_attributes);
                echo "----------get different attribute--------------\n";
                foreach ($custom_attributes as $keys => $values) {
                    
                    $attribute_code = $values['attribute_code'];

                    $value_value = $values['value'];
                    if("$attribute_code" == "texture"){
                        $texture = attribute($Mazentop,$attribute_code,$value_value);//花型
                         $zuzhuangarrary['texture'] =  $texture;
                         echo "花型$texture\n";
                    }elseif ("$attribute_code" == "items_package") {
                        $items_package = attribute($Mazentop,$attribute_code,$value_value);//打包方式
                        $zuzhuangarrary['items_package'] =  $items_package;
                        echo "打包方式$items_package\n";
                    }elseif ("$attribute_code" == "type") {
                        $country_type = attribute($Mazentop,$attribute_code,$value_value);//国家
                        echo "国家$country_type\n";
                       if("$country_type" !== "All"){
                        $zuzhuangarrary['country_type'] =  $country_type;
                       }
                        
                    }elseif ("$attribute_code" == "grade") {
                        $grade = attribute($Mazentop,$attribute_code,$value_value);//等级
                        $zuzhuangarrary['grade'] =  $grade;
                        echo "等级$grade\n";
                    }elseif ("$attribute_code" == "lace_size") {
                        $lace_size = attribute($Mazentop,$attribute_code,$value_value);//lace尺寸
                        if(strpos($lace_size,'4X4') !== false){
                                $zuzhuangarrary['lace_size'] =  "4\" x 4\"";
                            }elseif (strpos($lace_size,'4*4') !== false) {
                                $zuzhuangarrary['lace_size'] =  "4\" x 4\"";
                            }elseif (strpos($lace_size,'4X13') !== false) {
                                $zuzhuangarrary['lace_size'] =  "4\" x 13\"";
                            }elseif (strpos($lace_size,'4*13') !== false) {
                                $zuzhuangarrary['lace_size'] =  "4\" x 13\"";
                            }elseif (strpos($lace_size,'6X12') !== false) {
                                $zuzhuangarrary['lace_size'] =  "6\" x 12\"";
                            }elseif (strpos($lace_size,'6*12') !== false) {
                                $zuzhuangarrary['lace_size'] =  "6\" x 12\"";
                            }elseif (strpos($lace_size,'3*12') !== false) {
                                $zuzhuangarrary['lace_size'] =  "3\" x 12\"";
                            }elseif (strpos($lace_size,'3X12') !== false) {
                                $zuzhuangarrary['lace_size'] =  "3\" x 12\"";
                            }else{
                                $zuzhuangarrary['lace_size'] = $lace_size;
                            }
                        echo "lace尺寸$lace_size\n";
                    }elseif ("$attribute_code" == "color") {
                        $color = attribute($Mazentop,$attribute_code,$value_value);//颜色
                        $zuzhuangarrary['color'] =  $color;
                        echo "颜色$color\n";
                    }elseif ("$attribute_code" == "density") {
                        $density = attribute($Mazentop,$attribute_code,$value_value);//密度 
                       echo "密度$density\n";
                        $zuzhuangarrary['density'] =  $density;
                    }elseif ("$attribute_code" == "size") {
                        //$size = attribute($Mazentop,$attribute_code,$value_value);//密度 
                        $zuzhuangarrary['size'] =  $value_value;
                       echo "大小$value_value\n";
                    }

                }
                //处理 参数不存在问题  lace_size
                $if_is_str = array_key_exists('lace_size', $zuzhuangarrary);
                        if($if_is_str == 0){
                            echo " lace size 不存在 设置为空\n";
                            $zuzhuangarrary['lace_size'] =  '';
                        }else{
                            echo "lace size 存在 \n";
                        }
                $post_arr = postsku_getproductopptionid($zuzhuangarrary,$Mazentop,$options_arr,$db,$storeid_sku);
                Getinsert($arrData=$post_arr,$table='dlz_product_attr',$db,$sync_data="0");
               // postsku_getproductopptionid($zuzhuangarrary,$Mazentop,$options_arr,$db,$storeid_sku);
                //print_r($zuzhuangarrary);
 

        

     echo "++++++++++++++++++end++++++++++++++++++++\n"  ; 

}

//通过code  提取信息
function attribute($Mazentop,$attribute_code,$value_value){
    echo "$attribute_code\n";
        $Attribute = $Mazentop->getproductAttributeCode($attribute_code);

        foreach ($Attribute as $key_attr => $value_attr) {
                $texture_label = $value_attr['label'];
                $texture_value = $value_attr['value'];
                if($value_value == $texture_value){
                    $label = $texture_label;
                } 
            }

            return $label;
        
}

//通过sku 获取产品属性
function postsku_getproductopptionid($zuzhuangarrary,$Mazentop,$options_arr,$db,$storeid_sku){
    echo "******get option id*******\n";
    $options = $options_arr;//产品 options
    foreach ($options as $key => $value) {
               $product_sku = $value['product_sku'];//sku
               $title = $value['title'];//属性名称
               $sort_order = $value['sort_order'];//订单分类id
               $is_require = $value['is_require'];//
               $getoption_id = $value['option_id'];//id
               $option_values = $value['values'];//id
               if("$title" == "Part Design"){
                echo "----------Part Design--------------\n";
                $zuzhuangarrary['part_design_option_id'] = $getoption_id;
                   gettitle($getoption_id,$option_values,$field="part_design_option_id",$db,$table="dlz_product_partdesign",$storeid_sku);//
               }elseif ("$title" == "Hair Closure Length/inch") {
                 echo "----------Hair Closure Length/inch--------------\n";
                 $zuzhuangarrary['Closure_inch_option_id'] = $getoption_id;
                   gettitle($getoption_id,$option_values,$field="Closure_inch_option_id",$db,$table="dlz_product_Closureinch",$storeid_sku);//
               }elseif ("$title" == "Hair Weft Length/inch") {
                 echo "----------Hair Weft Length/inch--------------\n";
                 $zuzhuangarrary['Weft_inc_option_id'] = $getoption_id;
                   gettitle($getoption_id,$option_values,$field="Weft_inc_option_id",$db,$table="dlz_product_Weftinc",$storeid_sku);//
               }elseif (stripos($title,"Stretched")!== false) {
                echo "----------Stretched Length/Inch-------------\n";
                $zuzhuangarrary['Stretched_inc_option_id'] = $getoption_id;
                  gettitle($getoption_id,$option_values,$field="Stretched_inc_option_id",$db,$table="dlz_product_Stretchedinc",$storeid_sku);//
               }elseif ("$title" == "Type") {
                echo "----------Type-------------\n";
                $zuzhuangarrary['country_type_option_id'] = $getoption_id;
                   gettitle($getoption_id,$option_values,$field="country_type_option_id",$db,$table="dlz_product_countrytype",$storeid_sku);//
               }
           }
           return $zuzhuangarrary;

}


//获取一个option  id 下的 所有信息
function gettitle($getoption_id,$option_values,$field,$db,$table,$storeid_sku){
    $table_table = $table;
    //$filed_optionid = array();
  //  $filed_optionid['storeid_sku'] = $storeid_sku;
    //$filed_optionid[$field] = $getoption_id;
    //插入 optionid
    //echo "-------插入Option id ：$getoption_id-----------\n";
    //Getinsert($arrData=$filed_optionid,$table='dlz_product_attr',$db,$sync_data="0");
    $option_arrary = array();

    foreach ($option_values as $key_r => $value_r) {
            $UpdateTime = date('Y-m-d H:i:s');
            $option_arrary['UpdateTime'] = $UpdateTime; 
            $values_title = $value_r['title'];
            $option_arrary['title'] = $values_title;
            $values_price = $value_r['price'];
            $option_arrary['price'] = $values_price;
            $values_option_type_id = $value_r['option_type_id'];
            $option_arrary['storeid_sku'] = $storeid_sku.'-'.$values_option_type_id;
            $option_arrary['option_type_id'] = $values_option_type_id;
            echo "-------$values_title ---- $values_option_type_id-----------\n";
            Getinsert($arrData=$option_arrary,$table=$table_table,$db,$sync_data="0");
        }
      
        
        //print_r($option_arrary);
}

$db = null;
echo "last setp: Exit mysql success ! \n";